import axios from "../../http/axios.js";
import {
  apiGetOrderDetail,
  apiSiccessService
} from "../../http/server.js";
import {
  formatTime
} from "../../utils/util.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    arriveTime: null,
    dialog: {
      show: false,
      isText: false,
      title: "输入服务码",
      text: "",
      inputValue: "",
      maxlength: 15,
      placeholder: "请输入患者客户端的服务码"
    },
    btnObj: {
      btnShow: false, // 按钮的显示隐藏
      btnText: "服务完成"
    },
    totalList: [{
        title: "订单信息",
        list: [{
            key: "用户姓名",
            value: "张少峰"
          },
          {
            key: "身份证号",
            value: "张少峰"
          }
        ]
      },
      {
        title: "服务信息",
        list: [{
            key: "与用户关系",
            value: "张少峰"
          },
          {
            key: "身份证号",
            value: "张少峰"
          }
        ]
      },
      {
        title: "患者信息",
        list: [{
            key: "与用户关系",
            value: "张少峰"
          },
          {
            key: "身份证号",
            value: "张少峰"
          }
        ]
      }
    ], //头部下边的数组list
    title: {
      key: "服务未开始",
      value: "还剩2天多少小时"
    } //头部数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getDetail(options.orderNo);
    this.setDialog();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},
  // 设置初始化的弹出框
  setDialog() {
    let dialog = {
      show: false,
      isText: false,
      title: "输入服务码",
      text: "",
      inputValue: "",
      maxlength: 15, // 服务输入长度控制
      placeholder: "请输入患者客户端的服务码"
    }
    this.setData({
      dialog
    })
  },
  // 点击服务完成
  onClickSiccess() {
    let {
      dialog
    } = this.data;
    dialog.show = true;
    this.setData({
      dialog
    })
  },
  // 输入框输入进行数据写入
  handleInput(e) {
    let {
      dialog
    } = this.data;
    dialog.inputValue = e.detail;
    this.setData({
      dialog
    })
  },
  // 点击弹出框的确认
  onConfirm() {
    switch (this.data.dialog.isText) {
      case true: // true是文本提示
        {
          break;
        }
      case false: // false是输入框
        {
          let arriveTime = this.data.arriveTime; // 到达时间不存在没法直接进行点击服务完成
          if (!arriveTime) {
            wx.showToast({
              title: '请您先确认您安全到达,再点击服务完成',
              icon: "none",
              duration: 2000
            })
            return;
          }
          let orderNo = this.data.totalList[0].list[2].value
          let {
            dialog
          } = this.data;
          let inputValue = dialog.inputValue;
          axios({
            url: apiSiccessService,
            data: {
              orderNo,
              reservationNumber: inputValue
            }
          }).then(res => {
            wx.showToast({
              title: "服务完成,您辛苦了",
              icon: "none",
              duration: 1500
            })
            wx.setStorageSync("titleIndex", "2")
            setTimeout(() => {
              wx.navigateBack({
                url: '/pages/n_services/n_services',
              })
            }, 1500)
          })
          break;
        }
      default:
        break;
    }
  },
  // 获取订单即服务详情
  getDetail(orderNo) {
    axios({
      url: apiGetOrderDetail,
      data: {
        orderNo
      }
    }).then(res => {
      // console.log("71res", res)
      this.processingData(res)
    })
  },
  // 处理数据
  processingData(data) {
    let title = {};
    let btnObj = {};
    switch (Number(data.order.status)) {
      case 1:
        {
          break;
        }
      case 2:
        {
          break;
        }
      case 3: // 待服务
        {
          btnObj = {
            btnShow: false, // 按钮的显示隐藏
            btnText: "服务完成"
          };
          title = {
            key: "服务未开始",
            value: "服务时间：" + formatTime(new Date(data.order.serviceTime)).replace(/\//g, "-")
          }
          break;
        }
      case 4: // 进行中
        {
          btnObj = {
            btnShow: true, // 按钮的显示隐藏
            btnText: "服务完成"
          };
          title = {
            key: "服务进行中",
            value: "输入服务码即可完成服务"
          }
          break;
        }
      case 5: // 已完成
        {
          btnObj = {
            btnShow: false, // 按钮的显示隐藏
            btnText: "服务完成"
          };
          title = {
            key: "服务已完成",
            value: "服务完成后，患者即可评价"
          }
          break;
        }
      case 6:
        {
          break;
        }
      case 7: // 用户个人取消
        {
          btnObj = {
            btnShow: false, // 按钮的显示隐藏
            btnText: "服务完成"
          };
          title = {
            key: "服务已取消",
            value: "由于患者个人原因，服务已取消"
          }
          break;
        }
      case 8: // 院方取消
        {
          btnObj = {
            btnShow: false, // 按钮的显示隐藏
            btnText: "服务完成"
          };
          title = {
            key: "服务已取消",
            value: "由于院方原因，服务已取消"
          }
          break;
        }
      case 9:
        {
          break;
        }
      default:
        break
    }
    let totalList = [{
        title: "订单信息",
        list: [{
            key: "用户姓名",
            value: data.order.userName
          },
          {
            key: "身份证号",
            value: data.user.idCard
          },
          {
            key: "订单编号",
            value: data.order.orderNo
          },
          {
            key: "出发时间",
            value: data.order.leaveTime ? formatTime(new Date(data.order.leaveTime)).replace(/\//g, "-") : "未确认出发时间"
          },
          {
            key: "到达时间",
            value: data.order.arriveTime ? formatTime(new Date(data.order.arriveTime)).replace(/\//g, "-") : "未确认到达时间"
          },
          {
            key: "完成时间",
            value: data.order.completeTime ? formatTime(new Date(data.order.completeTime)).replace(/\//g, "-") : "未确认完成时间"
          }
        ]
      },
      {
        title: "服务信息",
        list: [{
            key: "服务项目",
            value: data.serviceItems.name
          },
          {
            key: "服务耗材",
            value: data.order.consumablesName
          },
          {
            key: "服务地址",
            value: data.order.serviceAddress
          },
          {
            key: "服务时间",
            value: formatTime(new Date(data.order.serviceTime)).replace(/\//g, "-")
          }
        ]
      },
      {
        title: "患者信息",
        list: [{
            key: "患者姓名",
            value: data.patient.userName
          },
          {
            key: "身份证号",
            value: data.patient.idCard
          },
          {
            key: "患者性别",
            value: data.patient.sex == 1 ? "男" : "女"
          },
          {
            key: "与用户关系",
            value: data.patient.relation
          }
        ]
      }
    ]
    let arriveTime = data.order.arriveTime;
    this.setData({
      totalList,
      title,
      btnObj,
      arriveTime
    })
  }
})